package com.cw.jeeyt.service.impl.facade;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.service.bo.facade.FacadeInfoBo;
import com.cw.jeeyt.service.converter.facade.FacadeInfoConverter;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeInfoDao;
import com.cw.jeeyt.dao.model.facade.FacadeInfoDo;
import com.cw.jeeyt.dao.query.facade.FacadeInfoQuery;
import com.cw.jeeyt.service.facade.FacadeInfoService;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 接口信息接口业务实现类
 *
 * @author chenw
 * @date 2019-11-01 23:46:50
 */
@Service
public class FacadeInfoServiceImpl implements FacadeInfoService {
    @Autowired
    private FacadeInfoDao facadeInfoDao;

    /**
     * 新增接口信息
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addFacadeInfo(FacadeInfoBo request) {
        if (request == null) {
            return null;
        }
        request.setCreateBy(SessionUtil.getSessionUserId());
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setCreateDate(new Date());
        request.setUpdateDate(new Date());
        facadeInfoDao.insertBySelective(FacadeInfoConverter.bo2Do(request));
        return "";
    }

    /**
     * 更新接口信息
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyFacadeInfo(FacadeInfoBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setUpdateDate(new Date());
        facadeInfoDao.updateBySelective(FacadeInfoConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除接口信息
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFacadeInfo(FacadeInfoBo request) {
        if (request == null) {
            return false;
        }
        return facadeInfoDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询接口信息
     *
     * @param request 条件
     * @return PageInfo<FacadeInfoBo>
     */
    @Override
    public PageInfo<FacadeInfoBo> selectPage(FacadeInfoBo request, PageParam pageParam) throws Exception {
        PageInfo<FacadeInfoDo> pageInfo = facadeInfoDao.selectPage(FacadeInfoQuery.builder()
            .id(request.getId())
            .docId(request.getDocId())
            .facadeCode(request.getFacadeCode())
            .facadeName(request.getFacadeName())
            .facadeRequestType(request.getFacadeRequestType())
            .facadeVersion(request.getFacadeVersion())
            .facadeUrl(request.getFacadeUrl())
            .operateType(request.getOperateType())
            .requests(request.getRequests())
            .responses(request.getResponses())
            .useMethod(request.getUseMethod())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build(), pageParam);
        List<FacadeInfoBo> facadeInfos = FacadeInfoConverter.doList2BoList(pageInfo.getList());
        if (CollectionUtil.isNotEmpty(facadeInfos)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("facadeRequestType", "enum_request_type");
            dictMap.put("operateType", "enum_operate_type");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(facadeInfos)), dictMap);
            facadeInfos = JSON.parseArray(jsonArray.toJSONString(), FacadeInfoBo.class);
        }
        return new PageInfo<>(pageInfo, facadeInfos);
    }

    /**
     * 查询接口信息详情信息
     *
     * @param request 条件
     * @return FacadeInfoBo
     */
    @Override
    public FacadeInfoBo queryFacadeInfoDetail(FacadeInfoBo request) {
        if (request == null) {
            return null;
        }
        return FacadeInfoConverter.do2Bo(facadeInfoDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询接口信息
     *
     * @param request 条件
     * @return List<FacadeInfoBo>
     */
    @Override
    public List<FacadeInfoBo> queryFacadeInfoList(FacadeInfoBo request) {
        if (request == null) {
            return null;
        }
        return FacadeInfoConverter.doList2BoList(facadeInfoDao.selectByExample(FacadeInfoQuery.builder()
            .id(request.getId())
            .docId(request.getDocId())
            .facadeCode(request.getFacadeCode())
            .facadeName(request.getFacadeName())
            .facadeRequestType(request.getFacadeRequestType())
            .facadeVersion(request.getFacadeVersion())
            .facadeUrl(request.getFacadeUrl())
            .operateType(request.getOperateType())
            .requests(request.getRequests())
            .responses(request.getResponses())
            .useMethod(request.getUseMethod())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build()));
    }

    /**
     * 根据key查询接口信息
     *
     * @param id
     * @return
     */
    @Override
    public FacadeInfoBo selectByKey(Long id) {

        return FacadeInfoConverter.do2Bo(facadeInfoDao.selectByKey(id));
    }
}
